#include <iostream>
using namespace std;

int a[10] = {10,15,12,18,20,32,25,14,16,23};
int n=10;


void show(int p, int q)
{ for(int i=p; i<=q; i++)
    cout << a[i] << " ";
  cout << endl;
}

void moveDown(int i, int k)
{ int p = 2*i+1;
  if(p<k)
  { if(p+1<k && a[p]<a[p+1]) p++;
    if(a[i]<a[p])
    { swap(a[i],a[p]);
      moveDown(p,k);
    }
  }
}


void heapsort()
{ for(int i=(n-2)/2; i>=0; i--)
    moveDown(i,n);
    
//  show(0,n-1);   
    
  for(int k=n; k>1; k--)
  { swap(a[0],a[k-1]); 
    moveDown(0,k-1);
  }
}


int main()
{
  show(0,n-1);   
  heapsort();
  show(0,n-1);
  
  return 0;
}

